package com.cskaoyan.controller.search;

import com.cskaoyan.model.vo.BaseRespVo;
import com.cskaoyan.model.vo.wxsearch.SearchIndexData;
import com.cskaoyan.service.search.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @program: project-market
 * @description:
 * @author: zhijie
 * @create: 2021-12-02 14:46
 **/
@RestController
@RequestMapping("wx/search")
public class WxSearchController {
    @Autowired
    SearchService searchService;

    /**
     * 获取当前登录用户的搜索历史,未登录则不显示用户的搜索历史,都显示热门搜索,以及默认关键字
     * 热门搜索根据sort_order字段升序排序
     * 搜索历史根据更新时间降序排序
     *
     * @return
     */
    @GetMapping("index")
    public BaseRespVo searchIndex(HttpSession session) {
        Integer id = (Integer) session.getAttribute("uid");
        SearchIndexData data = searchService.searchIndex(id);
        return BaseRespVo.ok(data);
    }

    /**
     * 用户输入的关键词模糊查询给出提示
     * 根据sort_order字段升序排序
     *
     * @param keyword
     * @return
     */
    @GetMapping("helper")
    public BaseRespVo searchHelper(String keyword) {
        List<String> data = searchService.searchHelper(keyword);
        return BaseRespVo.ok(data);
    }

    /**
     * 将用户的搜索记录逻辑删除
     *
     * @param session
     * @return
     */
    @PostMapping("clearhistory")
    public BaseRespVo searchClearhistory(HttpSession session) {
        Integer id = (Integer) session.getAttribute("uid");
        searchService.searchClearhistory(id);
        return BaseRespVo.ok();
    }

}
